 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name search-item">
        <div class="name width2">用户管理</div>
        <div class="mr-15">
          <el-button size="medium" type="primary" @click="add">新 增</el-button>
          <el-button size="medium" type="danger">刷 新</el-button>
        </div>
      </div>
      <div class="content-con">
        <div class="form-item">
            <el-table
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              class="table"
              stripe border
              :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }">
              <el-table-column prop="user_no" label="用户编号">
              </el-table-column>
              <el-table-column prop="user" label="用户名称">
              </el-table-column>
              <el-table-column prop="user_type" label="用户类别">
              </el-table-column>
              <el-table-column prop="phone" label="电话号码">
              </el-table-column>
              <el-table-column prop="add_time" label="创建时间">
              </el-table-column>
              <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.status == 0" type="success">已启用</el-tag>
                  <el-tag v-else type="warning">已禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="text" @click="handleEdit(scope.row)">
                    <i class="el-icon-edit"></i>编辑
                  </el-button>
                  <el-button type="text" @click.native.prevent="deleteRow(scope.$index,tableData)">
                    <i class="el-icon-delete"></i>删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        <!-- 分页-页码 -->
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableData.length">
          </el-pagination>
        </div>
      </div>
      <el-dialog
        :title="text"
        :visible.sync="dialogAdd"
        width="30%">
        <el-form :model="addForm" ref="addForm" size="medium">
          <el-form-item label="用户名称:" prop="user">
            <el-input v-model="addForm.user" placeholder="请输入用户名称"></el-input>
          </el-form-item>
          <el-form-item label="电话号码:" prop="phone">
            <el-input v-model="addForm.phone" placeholder="请输入电话号码"></el-input>
          </el-form-item>
          <el-form-item label="登录密码:" prop="pwd">
            <el-input v-model="addForm.pwd" placeholder="不修改请留空" type="password"></el-input>
            <span class="grey">请填写6到12位密码</span>
          </el-form-item>
          <el-form-item label="用户类别:" prop="user_type">
            <el-select v-model="addForm.user_type" style="width: 100%;">
              <el-option v-for="(item,index) in rolesData" :value="item.role_name" :key="index" :label="item.role_name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">已启用</el-radio>
              <el-radio :label="1">已禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="success" @click="save">保 存</el-button>
          <el-button @click="close">取 消</el-button>
        </span>
      </el-dialog>
   </el-card>

 </template>
<script>

export default {
  data () {
    return {
      dialogAdd: false,
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      //tableData: [] //表格数据
      text:'',
      tableData: [
        {
          user_no: '25',
          user: '张三',
          user_type: '管理员',
          phone: '19980019001',
          add_time: '2021-02-19 02:14:43',
          status: 0
        }
      ],
      addForm: {
        user: '',
        phone: '',
        pwd: '',
        user_type: '',
        status: ''
      },
      rolesData: [
        {
          id: 1,
          level: 1,
          role_name: "管理员",
          rules: "10,9,7,23,22,21,20,19,18,6,5,17,16,15,14,4,12,13,11,3,2,1,0",
          rulesMenu: "商户管理--线下充值纪录--收款账户--代付管理--数据分析--商户对账单--系统管理--所有商户",
          status: 0
        },
        {
          id: 2,
          level: 1,
          role_name: "总经理",
          rules: "10,9,7,4,12,13,11,3,2,1,0",
          rulesMenu: "商户管理--线下充值纪录--收款账户--代付管理--数据分析",
          status: 0
        }
      ]
    }
  },

  methods: {
    //保存表单
    save() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          console.log(this.addForm)
          this.addForm = {
            user: '',
            phone: '',
            pwd: '',
            user_type: '',
            status: ''
          }
          console.log(this.addForm)
          this.dialogAdd = false;
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },
    //对话框-取消/关闭
    close() {
      this.dialogAdd = false;
      this.addForm = {
        user: '',
        phone: '',
        pwd: '',
        user_type: '',
        status: ''
      }
    },

    //编辑操作
    handleEdit(row) {
      this.dialogAdd = true;
      this.text= "修改用户"
      this.addForm = {
        user: row.user,
        phone: row.phone,
        pwd: '',
        user_type: row.user_type,
        status: row.status
      }
    },

    //新增
    add() {
      this.dialogAdd = true;
      this.text = "新增用户"
    },

    //删除操作
    deleteRow(index, rows){
      rows.splice(index, 1);
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
